package com.example.service.impl;

import com.example.vo.RoleVo;
import com.example.entity.Role;
import com.example.repository.BaseRepository;
import com.example.repository.RoleRepository;
import com.example.service.RoleService;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl extends BaseReactiveServiceImpl<Role> implements RoleService {

    @Autowired
    RoleRepository roleRepository;

    @Autowired
    Mapper mapper;

    @Override
    protected BaseRepository<Role> getRepository() {
        return roleRepository;
    }

    @Override
    public Mono<Role> findByRoleName(String roleName) {
        return async(() -> roleRepository.findByRoleName(roleName));
    }

    @Override
    public Flux<RoleVo> findRoles(String userId) {
        List<Role> roles = roleRepository.findAll();
        List<RoleVo> roleVos = new ArrayList<>();
        for (Role role : roles) {
            RoleVo roleVo = mapper.map(role, RoleVo.class);
            roleVos.add(roleVo);
        }
        return async(roleVos);
    }
}
